package com.youhome.business.controller;

import com.youhome.common.exception.CustomException;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.common.response.ResultPage;
import com.youhome.service.model.Principal;
import com.youhome.service.service.BSendRecordMailGatherService;
import com.youhome.service.service.BSendRecordMailService;
import com.youhome.service.service.BTemplateMailService;
import com.youhome.service.service.MailService;
import com.youhome.utils.ValidationUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * Created by chen365 on 2017/12/6.
 */
@RestController
@Api(tags = "邮件")
public class MailController {

    @Autowired
    private BTemplateMailService bTemplateMailService;
    @Autowired
    private MailService mailService;
    @Autowired
    private BSendRecordMailService bSendRecordMailService;

    @Autowired
    private BSendRecordMailGatherService bSendRecordMailGatherService;

    @ApiOperation(value = "发邮件", notes = "发邮件", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "mails", value = "mails", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "Long", name = "tempId", value = "模版ID", required = true, paramType = "query")
    })
    @PostMapping(value = "/mail/send2")
    public Object sendMail(String mails, Long tempId,@ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        mailService.sendMail(principal,tempId, mails);
        return new Result(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "邮件模版", notes = "邮件模版", response = Result.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParam(dataType = "int", name = "type", value = "模版类型", paramType = "query")
    @GetMapping(value = "/mail/template/list")
    public Object templateList(Integer type) throws CustomException {
        return new Result(ResultCode.SUCCESS, bTemplateMailService.selectMailTemplate(type));
    }

    @ApiOperation(value = "邮件模版详情", notes = "邮件模版详情", response = Result.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParam(dataType = "Long", name = "id", value = "模版ID", paramType = "query")
    @GetMapping(value = "/mail/template/details")
    public Object templateDetails(Long id) throws CustomException {
        return new Result(ResultCode.SUCCESS, bTemplateMailService.selectByPrimaryKey(id));
    }

    @ApiOperation(value = "邮件历史记录列表", notes = "邮件历史记录列表", response = ResultPage.class)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "pageNumber", value = "当前页", paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "pageSize", value = "每页条数", paramType = "query")
    })
    @GetMapping(value = "/mail/history/list")
    public Object historyList(Integer pageNumber, Integer pageSize, @ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        return new ResultPage(ResultCode.SUCCESS, bSendRecordMailGatherService.selectByBean(principal,pageNumber, pageSize));
    }

    @ApiOperation(value = "邮件历史记录下列表详情", notes = "邮件历史记录下列表详情", response = ResultPage.class)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "pageNumber", value = "当前页", paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "pageSize", value = "每页条数", paramType = "query"),
            @ApiImplicitParam(dataType = "Long", name = "gatherId", value = "记录集Id", required = true, paramType = "query")
    })

    @GetMapping(value = "/mail/history/gather/list")
    public Object historyGatherList(Integer pageNumber, Integer pageSize, Long gatherId,
                                    @ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        return new ResultPage(ResultCode.SUCCESS, bSendRecordMailService.selectByBean(principal,gatherId, pageNumber, pageSize));
    }

    @ApiOperation(value = "邮件发送历史记录详情", notes = "邮件发送历史记录详情", response = Result.class)
    @ApiImplicitParam(dataType = "Long", name = "id", value = "记录ID", required = true, paramType = "query")
    @GetMapping(value = "/mail/history/details")
    public Object historyDetails(Long id) {
        return new Result(ResultCode.SUCCESS, bSendRecordMailService.selectByPrimaryKey(id));
    }

    @ApiOperation(value = "发邮件给代理商", notes = "发邮件给代理商", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping(value = "/sendToAgency")
    public Object sendToAgency(String email, String content) {
        if (StringUtils.isEmpty(email)) {
            return new Result<>(ResultCode.ERROR_EMAIL);
        }
        if (!ValidationUtils.isEmail(email)) {
            return new Result<>(ResultCode.ERROR_EMAIL);
        }
        if (StringUtils.isEmpty(content)) {
            return new Result<>(ResultCode.EMPTY_PARAM);
        }
        return new Result<>(ResultCode.SUCCESS);
    }
}
